package com.byzk.p2p.admin.bank.controller;

import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.bank.dto.BankCard;
import com.byzk.p2p.admin.bank.service.BankCardService;
import com.byzk.p2p.admin.bank.vo.BankCardVo;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.city.dto.City;
import com.byzk.p2p.admin.city.service.CityService;
import com.byzk.p2p.admin.money.util.BaofuUtil;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.service.UserService;

@Controller
@RequestMapping("/admin/bankCard")
public class BankCardController extends AdminBaseController {

	@Autowired
	private BankCardService bankCardService;
	@Autowired
	private CityService cityService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/list")
	public String listBankCard(Model model,@ModelAttribute BankCardVo bankCardVo) throws Exception{
		if (StringUtils.isNotBlank(bankCardVo.getUsername())) {
			bankCardVo.setUsername(java.net.URLDecoder.decode(bankCardVo.getUsername() , "UTF-8"));
			bankCardVo.setMatchUser(bankCardVo.getUsername());
			model.addAttribute("username", bankCardVo.getUsername());
		}
		if (StringUtils.isNotBlank(bankCardVo.getProvice())) {
			model.addAttribute("provice", bankCardVo.getProvice());
		}
		if (StringUtils.isNotBlank(bankCardVo.getCardNo())) {
			model.addAttribute("cardNo", bankCardVo.getCardNo());
		}
		PageInfo<BankCardVo> pageInfo = bankCardService.listAll(bankCardVo);
		if (CollectionUtils.isNotEmpty(pageInfo.getPageList())) {
			for (BankCardVo temp : pageInfo.getPageList()) {
				temp.setBankCode(BaofuUtil.bankMap.get(temp.getBankCode()));
			}
		}
		model.addAttribute("pageInfo", pageInfo);
		
		System.out.println(111);

		return ftl(Thread.currentThread().getStackTrace());
		
		
	}
	
	@RequestMapping(value="/detail",method = RequestMethod.POST)
	public String bankCardDetail(Model model,@RequestParam("id") Integer id) throws Exception{
		BankCard card = bankCardService.getById(id);
		User user = userService.getById(card.getUserId());
		super.printJSON(new JsonMsg(-1, "操作成功", card));
		return null;
	}
	
	
	@RequestMapping(value = "/bankCardDetail",method=RequestMethod.GET)
	public String bankCardInfo(Model model,@RequestParam("id") Integer id) throws Exception{
		BankCard card = bankCardService.getById(id);
		User user = userService.getById(card.getUserId());
		model.addAttribute("item", card);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 获取所有的省级城市
	 * @return
	 */
	private List<City> getOneLevelCitys(){
		City oneLevelCity = new City();
		//oneLevelCity.setPid("0001");
		List<City> oneLevelCitys = cityService.listAll(oneLevelCity);
		return oneLevelCitys;
	}
}
